package com.wzt.gift.asserts;

import com.wzt.gift.enums.ResponseEnum;
import com.wzt.gift.exception.BusinessException;

import java.util.List;

/**
 * @Author Zenith
 * @Date 2022/10/11 13:45
 * @Version 1.0
 */
public class Assert {
    /**
     * 断言对象不为空
     * @param obj
     * @param message
     */
    public static void objIsNotNull(Object obj, String message) {
        if (obj == null) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言字符串不为空
     * @param str
     * @param message
     */
    public static void strIsNotEmpty(String str, String message) {
        if (str == null || "".trim().equals(str)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言两个字符串相等
     * @param str1
     * @param str2
     * @param message
     */
    public static void strIsEqual(String str1, String str2, String message) {
        if (! str1.trim().equals(str2.trim())) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言List集合为空
     * @param list
     * @param response
     */
    public static void listIsEmpty(List<?> list, ResponseEnum response) {
        if (list != null && list.size() > 0) {
            throw new BusinessException(response.getCode(), response.getMessage());
        }
    }

    /**
     * 断言List集合不为空
     * @param list
     * @param response
     */
    public static void listIsNotEmpty(List<?> list, ResponseEnum response) {
        if (list == null || list.size() < 1) {
            throw new BusinessException(response.getCode(), response.getMessage());
        }
    }
}
